/************************** (C) COPYRIGHT 2019 Tang **************************
* File Name          : gpio_input.c
* Author             : Tang Yuan
* Version            : V1.0
* Date               : 09/12/2019
* Description        : gpio input handle
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "gpio_input.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

  __IO Union_uint8  bflagEvent;

 __IO Union_uint8 bPADATA;
 __IO Union_uint8 bPCDATA;
 __IO Union_uint8 bPDDATA;

 __IO Union_uint8 bStatusKeyLevel;

 __IO Union_uint8 bStatusKey;
 __IO Union_uint8  bStatusKeyTrigger;


uint8_t CountKeyConfirm;
__IO int8_t Encoder_Value = 5u, Encoder_Read, Encoder_Last;

__IO uint8_t PreStatusKey;
/* Private function prototypes -----------------------------------------------*/
void GPIO_Config(void);
/* Private functions ---------------------------------------------------------*/

void Proc_GPIO_Status(void)
{
  uint8_t ReadData = 0;
  
  PADATA = (uint8_t)(GPIOA->IDR);
  PCDATA = (uint8_t)(GPIOC->IDR);
  PDDATA = (uint8_t)(GPIOD->IDR);

  Key00 = PA3;
  Key01 = PD0;
  Key02 = PC0; // encoder key 
  Key03 = PC1; // inject key 

  if(PreStatusKey !=  StatusKey)
  {
    PreStatusKey = StatusKey;
    CountKeyConfirm = 0;
  }
  else if( (CountKeyConfirm++) >= TimeKeyConfirm )
  {
    ReadData = PreStatusKey ^ 0xFF; /* low state enbale */
    //ReadData = PreStatusKey;              /* */
    StatusKeyTrigger = (ReadData ^ StatusKeyLevel);
    StatusKeyLevel = ReadData;
  }

  /* update encoder  value */
  if((KeyTrigger00 == 1u)&&(KeyLevel00 == 0u) )
  {
    if(KeyLevel01 == 0u)
    {
      Encoder_Value ++;
    }
    else
    {
      Encoder_Value --;
    }
    
  }
  if(Encoder_Value > 10)
   Encoder_Value = 10;
  else if(Encoder_Value <1)
   Encoder_Value =1;
}

bool Gpio_get_key1_val( void )
{
  return bStatusKeyLevel.BIT.bit2;
}

bool Gpio_get_key2_val( void )
{
  return bStatusKeyLevel.BIT.bit3;
}

uint8_t Gpio_get_enc_val( void )
{
  return Encoder_Value;
}

void Gpio_set_enc_val( uint8_t val )
{
  Encoder_Value = val;
}
/**
 * 
LED0 PB7
LED7 PC4
LED4 PC3
LED5 PA2

LED6 PB3
LED9 PB1
LED8 PB2
LED3 PB4

LED2 PB5
LED1 PB6
 * */
uint16_t temp_odr;
Union_uint16 hLED;
uint16_t temp;
void Gpio_LED_Ctrl(uint8_t num)
{
  

  temp = 0x03FF >> (0x0A - num);
  
  hLED.Bvalue = temp ;

  temp_odr = hLED.BIT.bit0 << 7; //bit 0 LED0 PB7
 
  temp_odr += hLED.BIT.bit4 <<  3; //bit4 LED6 PB3
  temp_odr += hLED.BIT.bit5 <<  1; // LED9 PB1
  temp_odr += hLED.BIT.bit6 <<  2; // LED8 PB2
  temp_odr += hLED.BIT.bit7 <<  4; // LED3 PB4

  temp_odr += hLED.BIT.bit8 <<  5; // LED2 PB5
  temp_odr += hLED.BIT.bit9 <<  6; // LED1 PB6

  GPIOB->ODR = temp_odr & 0xFE;

  
  temp_odr  =  (hLED.BIT.bit1 << 4); // bit1 LED7 PC4 // LED4 PC3
  temp_odr +=  (hLED.BIT.bit2 << 3);
  GPIOC->ODR = temp_odr & 0x18;  
  
  if(hLED.BIT.bit3)// LED5 PA2
  {
    GPIO_SetBits(GPIOA, GPIO_Pin_2);
  }
  else
  {
    GPIO_ResetBits(GPIOA, GPIO_Pin_2);
  }
   
}

/**
 * @brief gpio init 
 * 
 * */
void GPIO_Config(void)
{
  
  /* Set Input mode  key1 key2 ena enb*/
  GPIOA->DDR = 0x00;
  GPIOC->DDR = 0x00;
  GPIOD->DDR = 0x00;

  /* Set external interrupt key1-PC0  key2-PC1*/
  GPIOC->CR2 = 0x03;
  
  /* Initialize the Interrupt sensitivity joystick */
  EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling);
  EXTI_SetPinSensitivity(EXTI_Pin_0, EXTI_Trigger_Falling);
  
  /*Output GPIO initial*/
  /*LED, & PWM output Low level */
   GPIOA->ODR =  0x00;
   GPIOB->ODR =  0x00;
   GPIOC->ODR =  0x00;

  /* Set Output mode */
  GPIOA->DDR |= GPIO_Pin_2;
  GPIOB->DDR =  0xFF;
  GPIOC->DDR |= GPIO_Pin_3| GPIO_Pin_4;
  /* Set ouput as push-pull */
  GPIOA->CR1 |= GPIO_Pin_2;
  GPIOB->CR1 =  0xFF;
  GPIOC->CR1 |= GPIO_Pin_3| GPIO_Pin_4;

}

/******************* (C) COPYRIGHT 2019 Tang  *********************/
